package org.dcarew.tado;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class TaDoPlugin
	extends AbstractUIPlugin
{
	public static final String	PLUGIN_ID	= "org.dcarew.tado";

	private static TaDoPlugin	plugin;
	
	
	/**
	 * The constructor
	 */
	public TaDoPlugin()
	{
	}

	public void start(BundleContext context)
		throws Exception
	{
		super.start(context);
		
		plugin = this;
	}

	public void stop(BundleContext context)
		throws Exception
	{
		plugin = null;
		
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static TaDoPlugin getPlugin()
	{
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path)
	{
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
	
	public static Image getImage(String path)
	{
		return getPlugin().getBundledImage(path);
	}
	
	/**
	 * Returns an image for the image file at the given plug-in relative path. Client do not need to
	 * dispose this image. Images will be disposed automatically.
	 * 
	 * @param path the path
	 * @return image instance
	 */
	public Image getBundledImage(String path)
	{
		Image image = getImageRegistry().get(path);
		
		if (image == null)
		{
			getImageRegistry().put(path, getImageDescriptor(path));
			
			image = getImageRegistry().get(path);
		}
		
		return image;
	}
	
}
